package br.uefs.dexa.rfframework.gui.utils;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Graphics;

import javax.imageio.ImageIO;
import javax.swing.JPanel;



public class ImagePanel extends JPanel {

	
	private static final long serialVersionUID = 1L;
	
	private int width;
	
    private int heigth;
    
    private BufferedImage image;
	
	public ImagePanel(String imagePath, int imageHeigth)
	{
		
		 try {                
	          image = ImageIO.read(new File(imagePath));
	          
	       } catch (IOException ex) {
	           ex.printStackTrace();
	    	   System.err.print(ex.getMessage());
	       }

		 this.heigth = imageHeigth;
		 this.width = getNormalizedWidth(imageHeigth);
		 
		 setPreferredSize(new Dimension(this.width, this.heigth));
	}
	
	public ImagePanel(String imagePath, double scale)
	{

		try {                
	          image = ImageIO.read(new File(imagePath));
	          
	       } catch (IOException ex) {
	            ex.printStackTrace();
	            System.err.print(ex.getMessage());
	       }

		 this.heigth = (int) Math.round( image.getHeight() * scale );
		 this.width = (int) Math.round( image.getWidth() * scale  ); 
		 
		 setPreferredSize(new Dimension(this.width, this.heigth));
	}
	
	private int getNormalizedWidth(int heigth)
	{
		return  (int) Math.round( (double) (( image.getWidth() * heigth ) / image.getHeight()) );
	}
	
	@Override
    protected void paintComponent(Graphics g) 
	{
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this.width, this.heigth, null);
    }
	
	public int getWidth()
	{
		return this.width;
	}
	
	public int getHeigth()
	{
		return this.heigth;
	}
	
}
